/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of OrderStore.
	/// </summary>
	public class OrderStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		public OrderStore(Site site,HtmlWeb client)
		{
			_site = site;
			_client = client;
		}
		
		public List<IDictionary> GetOrders()
		{
			string id = "";
			List<IDictionary> Orders = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders&module=orders");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Order = (IDictionary)recs[i];
						id = Order["orders_id"].ToString();
						Order = GetOrder(id);
						Order["orders_id"] = id;
						Order["order_products"] = GetOrderProducts(id);
						Order["orders_status"] = GetOrderStatus(id);
						Order["gift_certificates"] = GetGiftCertificates(id);
						Orders.Add(Order);
					}									
				}
			}			
			
			return Orders;
		}
		
		private List<IDictionary> GetOrderStatus(string OrderId)
		{
			List<IDictionary> OrderStatus = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders_status&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary orderStatus = (IDictionary)recs[i];
						OrderStatus.Add(orderStatus);												
					}									
				}
			}			
			
			return OrderStatus;
		}
		
		private IDictionary GetOrder(string OrderId)
		{
			List<IDictionary> entries = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=load_order&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{					
					IDictionary Order = (IDictionary)json[key];					
					entries.Add(Order);
				}
			}			
			
			return entries[0];
		}
		
		private List<IDictionary> GetOrderProducts(string OrderId)
		{
			List<IDictionary> OrderProducts = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_orders_edit_products&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary OrderProduct = (IDictionary)recs[i];
						OrderProducts.Add(OrderProduct);												
					}									
				}
			}			
			
			return OrderProducts;
		}		
		
		private List<IDictionary> GetGiftCertificates(string OrderId)
		{
			List<IDictionary> GiftCertificates = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_gift_certificates&module=orders&orders_id=" + OrderId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary GiftCertificate = (IDictionary)recs[i];
						GiftCertificates.Add(GiftCertificate);												
					}									
				}
			}			
			
			return GiftCertificates;
		}
	}
}
